<style>
    .table>thead>tr>th{text-align: center}
    .table>tbody>tr>td{vertical-align: middle; text-align: center}
</style>
<div id="page-wrapper">
    <div class="row">
        <div class="col-lg-12">
            <h3 class="page-header">
                <span>数据统计</span>
            </h3>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <ul class="nav nav-tabs" id="myTab">
                        <li><a href="{:U('User/Questionnaire/statistics',array('id' => $id))}">总概</a></li>
                        <li><a href="{:U('User/Questionnaire/statisticsDate',array('id' => $id))}">最新数据</a></li>
                        <li><a href="{:U('User/Questionnaire/statisticsUser',array('id' => $id))}">用户行为</a></li>
                        <li class="active"><a href="javascript:;">选题统计</a></li>
                    </ul>
                </div>
                <div class="panel-body">
                    <div class="form-horizontal">
                        <div class="tab-content">
                            <div class="tab-pane active">
                                <div class="panel-heading">
                                    <button type="button" onclick="return download();" class="btn btn-info">导出</button>
                                    <button type="button" onclick="return clearOption();" class="btn btn-default">清空</button>
                                </div>
                                <div class="panel-body">
                                    <div class="table-responsive">
                                        <table class="table table-bordered">
                                            <thead>
                                                <tr>
                                                    <th width="35%">题目</th>
                                                    <th width="35%">选项</th>
                                                    <th width="14%">选择次数</th>
                                                    <th width="14%">百分比</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                            foreach($list as $question){
                                                echo '<tr>';
                                                        echo '<td>' . $question['title'] . '</td>';
                                                        if(array_key_exists($question['id'], $option_arr)){
                                                            $count = $option_arr[$question['id']][0]['count'];
                                                            foreach($option_arr[$question['id']] as $key => $option){
                                                                if($key != 0){
                                                                    $count += $option['count'];
                                                                }
                                                            }
                                                            echo "<td>" . $option_arr[$question['id']][0]['title'] . "</td>";
                                                            echo "<td>" . $option_arr[$question['id']][0]['count'] . "</td>";
                                                            echo "<td>";
                                                            if($count == 0){
                                                                echo 0;
                                                            }else{
                                                                echo round($option_arr[$question['id']][0]['count'] / $count, 2);
                                                            }
                                                            echo "</td>";
                                                            echo '</tr>';

                                                            foreach($option_arr[$question['id']] as $key => $option){
                                                                if($key != 0){
                                                                    echo "<tr>";
                                                                    echo "<td></td><td>" . $option['title'] . "</td><td>" . $option['count'] . "</td>";
                                                                    echo "<td>";
                                                                    if($count == 0){
                                                                        echo 0;
                                                                    }else{
                                                                        echo round($option['count'] / $count, 2);
                                                                    }
                                                                    echo "</td>";
                                                                    echo "</tr>";
                                                                }
                                                            }
                                                        }else{
                                                            echo '</tr>';
                                                        }

                                            }
                                        ?>
                                            </tbody>
                                        </table>
                                    </div>
                                    <div class="pages">
                                        {$page}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(function () {
        $('#myTab a:eq(3)').tab('show');//初始化显示哪个tab
    })

    function download(){
        //var data = $("form").serialize();
        var id = '<?php echo $id; ?>';
        window.location = '/User/Questionnaire/exportOption?id=' + id;
        return false;
    }

    function clearOption(){
        if(!confirm('确认删除吗？')) return false;
        var id = '<?php echo $id; ?>';
        $.ajax({
            data: {id: id},
            dataType: 'json',
            url: '/User/Questionnaire/clearOption',
            type: 'get',
            success: function (res) {
                alert(res['info']);
                window.location.reload();
            }
        });
        window.location.reload();
    }

</script>